/*
 * The Yard Utilties - http://www.theyard.net/
 * 
 * Copyright (c) 2008 by Vlideshow, Inc..  All Rights Resrved.
 * 
 * This library is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License as published by the Free Software 
 * Foundation; either version 2.1 of the License, or (at your option) any later 
 * version. 
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along 
 * with this library; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package net.theyard.components.netstreamsm
{
  import net.theyard.components.Utils;
  import flash.net.NetStream;

  internal class StoppedState implements StreamState
  {
    private var stateMachine:NetStreamStateMachine;
    public function StoppedState(stateMachine:NetStreamStateMachine)
    {
      Utils.ytrace("Starting state: " + this.toString());;
      this.stateMachine=stateMachine;
    }

    public function toString():String
    {
      return NetStreamStateMachine.STATE_STOPPED;
    }

    public function play(ns:NetStream,name:Object, ...args):void
    {
      //Note: the second paramater - 0 - specifies an FLV file
      //the NetStream method is from Client Side
      //Communication ActionScript but works with AS 3.0
      //because ObjectEncoding is imported.
      if (ns != null)
      {
        Utils.ytrace("Begin playing: " + name.toString());
        stateMachine.setStreamInfo(name.toString(), null);
        stateMachine.gotoState(NetStreamStateMachine.STATE_STARTPLAY);
        args.unshift(name);
        ns.play.apply(ns, args);
      }
      else
      {
        Utils.ytrace("Must connect before you can play");
      }
    }

    public function publish(ns:NetStream,flv:String,publishType:String):void
    {
      if (ns != null)
      {
        Utils.ytrace("Begin publishing (" + publishType +"): " + flv);
        stateMachine.setStreamInfo(flv, publishType);
        stateMachine.gotoState(NetStreamStateMachine.STATE_STARTPUBLISH);
        ns.publish(flv,publishType);
      }
      else
      {
        Utils.ytrace("Must connect before you can publish");
      }
    }

    public function stop(ns:NetStream):void
    {
      Utils.ytrace("You're already stopped");
    }

    public function togglePause(ns:NetStream):void
    {
      Utils.ytrace("Must be playing to pause");
    }
    public function onDisconnect(ns:NetStream):void
    {
      var streamName:String = stateMachine.getCurrentStreamName();
      Utils.ytrace("got a stream disconnect: " + streamName == null?"":streamName);
      stateMachine.gotoState(NetStreamStateMachine.STATE_DISCONNECTED);
      if (ns)
      {
        ns.close();
      }
    }

    public function onPlayStart(ns: NetStream) : void
    {
      Utils.ytrace("Got unexpected onPlayStart.  Ignoring.");
    }

    public function onPublishStart(ns: NetStream) : void
    {
      Utils.ytrace("Got unexpected onPublishStart.  Ignoring.");
    }

    public function onPlayStop(ns: NetStream) :void
    {
      Utils.ytrace("Got unexpected onPlayStop.  Ignoring.");
    }

    public function onPublishStop(ns: NetStream) : void
    {
      Utils.ytrace("Got unexpected onPublishStop.  Ignoring.");
    }

    public function onReconnect() : void
    {
      Utils.ytrace("Stream already stopped so not resetting.");
    }
  }
}

